package test;

import java.sql.*;

public class DBAddProfTitle {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动程序
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/bysj" +
                "?useUnicode=true&characterEncoding=utf8" + //后面指定编码为utf8
                "&serverTimezone=Asia/Shanghai";//服务器时区为中国上海
        //用户名
        String username = "root";
        //密码
        String password = "h666niub";
        //获得连接的对象
        Connection connection = DriverManager.getConnection(url, username, password);
        //创建语句盒对象
        Statement stmt = connection.createStatement();
        //SQL语句
        String addSchool_sql1 = "INSERT INTO ProfTitle(no,description,remarks) VALUES" +
                "('01','教授','')";
        String addSchool_sql2 = "INSERT INTO ProfTitle(no,description,remarks) VALUES" +
                "('02','副教授','')";
        String addSchool_sql3 = "INSERT INTO ProfTitle(no,description,remarks) VALUES" +
                "('03','讲师','')";
        String addSchool_sql4 = "INSERT INTO ProfTitle(no ,description,remarks) VALUES" +
                "('04','助教','')";

        //使用SQL语句
        stmt.execute(addSchool_sql1);
        stmt.execute(addSchool_sql2);
        stmt.execute(addSchool_sql3);
        stmt.execute(addSchool_sql4);


        //执行SQL查询语句并获得结果集对象
        ResultSet resultSet = stmt.executeQuery("select * from ProfTitle");
        //如结果集仍然有下一条记录，则执行循环体
        while (resultSet.next()) {
            //打印结果集中当前记录的“id”字段值
            System.out.print(resultSet.getInt("id"));
            System.out.print(",");
            //打印结果集中当前记录的“description”字段值
            System.out.print(resultSet.getString("description"));
            System.out.print(",");
            //打印结果集中当前记录的“no”字段值
            System.out.print(resultSet.getString("no"));
            System.out.print(",");
            //打印结果集中当前记录的“remarks”字段值
            System.out.println(resultSet.getString("remarks"));
        }
        //关闭
        stmt.close();
        //关闭连接对象
        connection.clearWarnings();
    }
}